package com.itheima.tlias84.controller;


import com.itheima.tlias84.dto.SelectAllEmpDTO;
import com.itheima.tlias84.dto.SelectEmpDTO;
import com.itheima.tlias84.dto.SelectEmpDatailDTO;
import com.itheima.tlias84.po.InsertEmpParam;
import com.itheima.tlias84.po.SelectEmpPageParam;
import com.itheima.tlias84.po.UpdateEmpParam;
import com.itheima.tlias84.service.EmpService;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/emps")
@CrossOrigin
public class EmpController {
    @Resource
    EmpService empService;

    @DeleteMapping
    public ResultUtil testBatchDelete(@RequestParam("ids") List<Integer> ids){
        if (ids.isEmpty()){
            return ResultUtil.fail("请先选择id");
        }
        int flag = empService.deleteEmpByIds(ids);
        return ResultUtil.success(flag);
    }
    @PostMapping
    public ResultUtil testInsert(@Validated @RequestBody InsertEmpParam param){

        int flag = empService.insertEmp(param);
//        log.info(Objects.toString(flag));
        return flag==0?ResultUtil.fail("新增失败"):ResultUtil.success("新增成功");
    }

    //分页查询
    @GetMapping
    public ResultUtil selectEmpByPage(SelectEmpPageParam param) {
        if(Objects.isNull(param.getPage()) || Objects.isNull(param.getPageSize())){
            return ResultUtil.fail("分页参数不存在");
        }

//        List<SelectEmpDTO> dtos = empService.selectEmpByPage(param);
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("total",dtos.size());
//        map.put("rows",dtos);
        return empService.selectEmpByPage(param);
    }
    @GetMapping("/{id}")
    public ResultUtil selectEmpById(@PathVariable("id") Integer id) {
        SelectEmpDatailDTO dto = empService.selectEmpById(id);
        return ResultUtil.success(dto);
    }
    @PutMapping
    public ResultUtil updateEmp( @RequestBody UpdateEmpParam updateEmpParam){
        int flag = empService.updateEmp(updateEmpParam);
        return flag==0?ResultUtil.fail():ResultUtil.success();
    }
    @GetMapping("list")
   public ResultUtil selectAllEmp(){
        List<SelectAllEmpDTO> dtos = empService.selectAllEmp();
        return ResultUtil.success(dtos);
    }

}
